package com.example.smartcity1.mainfragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.MyUserInfoBean;
import com.example.smartcity1.my.MyFeedbackActivity;
import com.example.smartcity1.my.MyLoginActivity;
import com.example.smartcity1.my.MyPasswordActivity;
import com.example.smartcity1.my.MyUserInfoActivity;
import com.example.smartcity1.my.MyUserOrderActivity;
import com.example.smartcity1.network.RetrofitClient;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.smartcity1.network.Utils.login;

/**
 * 个人中心视图
 */
public class MyFragment extends Fragment {

    private static final String TAG = "MyFragment";

    private SharedPreferences user;

    private ImageView imageView;
    private TextView textName;
    private LinearLayout linearInfo;
    private LinearLayout linearOrder;
    private LinearLayout linearPassword;
    private LinearLayout linearFeedback;
    private Button button;

    public MyFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        initView(view);
        //判断是否登录
        if (login == 0) login();
        button.setOnClickListener(v -> {
            if (login == 0) {//登录
                login();
            } else if (login == 1) {//退出账号
                login = 0;
                SharedPreferences.Editor editor = user.edit();
                editor.putString("username", "");
                editor.putString("password", "");
                editor.putString("token", "");
                editor.putString("userId","");
                editor.apply();
                startActivity(new Intent(getActivity(), MyLoginActivity.class));
            }
        });
        linearInfo.setOnClickListener(v -> {
            if (login == 0) {
                login();
            } else {
                startActivity(new Intent(getActivity(), MyUserInfoActivity.class));
            }
        });
        linearOrder.setOnClickListener(v -> {
            if (login == 0) {
                login();
            } else {
                startActivity(new Intent(getActivity(), MyUserOrderActivity.class));
            }
        });
        linearPassword.setOnClickListener(v -> {
            if (login == 0) {
                login();
            } else {
                startActivity(new Intent(getActivity(), MyPasswordActivity.class));
            }
        });
        linearFeedback.setOnClickListener(v -> {
            if (login == 0) {
                login();
            } else {
                startActivity(new Intent(getActivity(), MyFeedbackActivity.class));
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (login == 0) {
            button.setText("登录");
        } else {
            button.setText("退出账号");
            getMyUserInfoData();
        }
    }

    private void initView(View view) {
        imageView = view.findViewById(R.id.my_home_img);
        textName = view.findViewById(R.id.my_home_name);
        linearInfo = view.findViewById(R.id.my_home_info_li);
        linearOrder = view.findViewById(R.id.my_home_order_li);
        linearPassword = view.findViewById(R.id.my_home_password_li);
        linearFeedback = view.findViewById(R.id.my_home_feedback);
        button = view.findViewById(R.id.my_home_button);

    }

    private void login() {
        Toast.makeText(getActivity(), "尚未登录, 请登录", Toast.LENGTH_SHORT).show();
        startActivity(new Intent(getActivity(), MyLoginActivity.class));
    }

    private void getMyUserInfoData() {
        RetrofitClient.appService().getMyUserInfoData(user.getString("token", "")).enqueue(new Callback<MyUserInfoBean>() {
            @Override
            public void onResponse(Call<MyUserInfoBean> call, Response<MyUserInfoBean> response) {
                MyUserInfoBean myUserInfoBean = response.body();
                if (myUserInfoBean != null && myUserInfoBean.getCode() == 200) {
                    MyUserInfoBean.UserDTO userDTO = myUserInfoBean.getUser();
                    textName.setText(userDTO.getNickName());
                    if (!userDTO.getAvatar().equals("")) {
                        Glide.with(Objects.requireNonNull(getActivity()))
                                .load(RetrofitClient.IP + userDTO.getAvatar())
                                .centerCrop()
                                .into(imageView);
                    }

                }
            }

            @Override
            public void onFailure(Call<MyUserInfoBean> call, Throwable t) {
            }
        });
    }
}